﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AzureCloudDriveBuddy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {

            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtFile.Text = dlg.FileName;
            }

        }

        private void btUpload_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAccount.Text) || string.IsNullOrEmpty(txtKey.Text) || string.IsNullOrEmpty(txtFile.Text))
            {
                // if stuff is missing display a message box in the purest 90s style
                MessageBox.Show("No soup for you", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //all clear
            Config config = new Config();
            config.Url = new Uri(txtDest.Text);
            config.Vhd = new FileInfo(txtFile.Text);

            if (!config.Vhd.Exists)
            {
                //the 90s called, they want their MessageBox back
                MessageBox.Show("The VHD file to upload doesn't exist. \nPlease select a file to upload", "Invalid file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            config.Key= txtKey.Text;

            //show the progress bar and disable downlad button
            lblup.Visible = true;
            prUpload.Visible = true;
            btDownload.Enabled = false;

            try
            {
                //upload ; need background worker
                PageBlobStuff.UploadVHDToCloud(config);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error uploading : " + exc.Message);
            }

            //hide the progress bar once upload is done
            lblup.Visible = false;
            prUpload.Visible = false;
            btDownload.Enabled = true;
        }

        private void btPrefill_Click(object sender, EventArgs e)
        {
            txtAccount.Text = Properties.Settings.Default.StorageAccount;
            txtKey.Text = Properties.Settings.Default.StorageKey;
            txtSource.Text = txtDest.Text = Properties.Settings.Default.ContainerURL;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //show the progress bar and disable the upload button
            lbldn.Visible = true;
            prDownload.Visible = true;
            btUpload.Enabled = false;

            MessageBox.Show("Not implemented","error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

            //hide the progress bar once upload is done
            lbldn.Visible = false;
            prDownload.Visible = false;
            btUpload.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtTargetFile.Text = dlg.SelectedPath;
            }
        }



    }
}
